package com.tcwlkj.uuService.ui.set;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.model.LoginModel;
import com.tcwlkj.uuService.ui.BaseActivity;
import com.tcwlkj.uuService.ui.SettingActivity;
import com.tcwlkj.uuService.ui.login.LoginActivity;
import com.tcwlkj.uuService.utils.ChcheUtils;
import com.tcwlkj.uuService.utils.Const;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.ToastUtil;
import com.tcwlkj.uuService.utils.Tools;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/10/18 0018.
 */

public class ChangePassWordActivity extends BaseActivity implements TextWatcher {
    @Bind(R.id.edit_oldps_changps)
    EditText edit_oldps_changps;
    @Bind(R.id.edit_newps01_changps)
    EditText edit_newps01_changps;
    @Bind(R.id.edit_newps02_changps)
    EditText edit_newps02_changps;
    @Bind(R.id.btn_changps)
    Button btn_changps;
    @Bind(R.id.iv_old)
    ImageView iv_old;
    @Bind(R.id.iv_new01)
    ImageView iv_new01;
    @Bind(R.id.iv_new02)
    ImageView iv_new02;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        ButterKnife.bind(this);
        initView();
        btn_changps.setClickable(false);
    }

    private void initView() {
        changeTitle("修改密码");
        edit_oldps_changps.addTextChangedListener(this);
        edit_newps01_changps.addTextChangedListener(this);
        edit_newps02_changps.addTextChangedListener(this);
    }

    boolean old_tag=true;
    boolean new01_tag=true;
    boolean new02_tag=true;

    @OnClick({R.id.btn_changps,R.id.iv_old,R.id.iv_new01,R.id.iv_new02})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_changps:
                    upass();
                break;
            case R.id.iv_old:
                if(old_tag){
                    iv_old.setImageDrawable(getResources().getDrawable(R.mipmap.visible));
                    old_tag=false;
                    edit_oldps_changps.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    iv_old.setImageDrawable(getResources().getDrawable(R.mipmap.invisible));
                    old_tag=true;
                    edit_oldps_changps.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.iv_new01:
                if(new01_tag){
                    iv_new01.setImageDrawable(getResources().getDrawable(R.mipmap.visible));
                    new01_tag=false;
                    edit_newps01_changps.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    iv_new01.setImageDrawable(getResources().getDrawable(R.mipmap.invisible));
                    new01_tag=true;
                    edit_newps01_changps.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.iv_new02:
                if(new02_tag){
                    iv_new02.setImageDrawable(getResources().getDrawable(R.mipmap.visible));
                    new02_tag=false;
                    edit_newps02_changps.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    iv_new02.setImageDrawable(getResources().getDrawable(R.mipmap.invisible));
                    new02_tag=true;
                    edit_newps02_changps.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
        }
    }
    private void changeEdit(){

    }

    private void upass() {
        String  edit_newps=edit_newps02_changps.getText().toString().trim();
        String edit_newps01=edit_newps01_changps.getText().toString().trim();
        String edit_old=edit_oldps_changps.getText().toString().trim();
        if(!edit_newps.equals(edit_newps01)){
            ToastUtil.getInstance().show("两次新密码不相同");
            return;
        }
        if (edit_newps.length() < 6) {
            ToastUtil.getInstance().show("密码长度不少于6位");
            return;
        }
        if(edit_newps.length()>20){
            ToastUtil.getInstance().show("密码长度不大于20位");
            return;
        }
        if (Tools.isContainChinese(edit_newps)) {
            ToastUtil.getInstance().show("密码不能包含汉字,请重新输入");
            return;
        }
        Call<CommonResponse> httpResultCall = RetrofitUtils.getRequest(this, true)
                .upass("uedit",3,"1111", ChcheUtils.getInstance().getDriverDetailModel().getBase().getMobile(),edit_newps,edit_old);
        httpResultCall.enqueue(new HttpCallBack<CommonResponse>() {
            @Override
            public void onSuccess(Object t) {
                logout();
                ToastUtil.getInstance().show("密码修改成功");
                PreferencesUtils.putBoolean(MyApp.getContext(), "Login", false);
                startActivity(LoginActivity.class);
                finish();
                Const.setting.finish();
                Const.main.finish();
            }
            @Override
            public void onFail(int code, String message) {
                if(code == 40104){
                    upass();
                }else {
                    ToastUtil.getInstance().show(message);
                }
            }
        });
    }
    private void logout() {
        Call<CommonResponse<Object>> httpResultCall = RetrofitUtils.getRequest(ChangePassWordActivity.this, false).logout("10002",PreferencesUtils.getString(MyApp.getContext(), "userID"));
        httpResultCall.enqueue(new HttpCallBack<CommonResponse<Object>>() {
            @Override
            public void onSuccess(Object t) {

            }
            @Override
            public void onFail(int code, String message) {

            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String edit_oldps=edit_oldps_changps.getText().toString().trim();
        String edit_newps01=edit_newps01_changps.getText().toString().trim();
        String edit_newps02=edit_newps02_changps.getText().toString().trim();
        if(!TextUtils.isEmpty(edit_oldps) && !TextUtils.isEmpty(edit_newps01) && !TextUtils.isEmpty(edit_newps02)){
            btn_changps.setClickable(true);
            btn_changps.setBackgroundResource(R.drawable.bg_btn_mainred);
        }else {
            btn_changps.setClickable(false);
            btn_changps.setBackgroundResource(R.drawable.bg_btn_gray);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
